package com.wechet.serveice.impl;

import com.wechet.DAO.ProductInfoDAO;
import com.wechet.common.ProductStatus;
import com.wechet.domain.ProductInfo;
import com.wechet.dto.CartDTO;
import com.wechet.exception.SellException;
import com.wechet.serveice.ProductInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by sc on 2017/9/20.
 */
@Service
public class ProductInfoServiceImpl implements ProductInfoService {

    @Autowired
    private ProductInfoDAO productInfoDAO;

    @Override
    public ProductInfo findOne(String productId) {
        return productInfoDAO.findOne(productId);
    }

    @Override
    public List<ProductInfo> findUpAll() {
        return productInfoDAO.findByProductStatus(ProductStatus.UP.getCode());
    }

    @Override
    public Page<ProductInfo> findAll(Pageable pageable) {
        return productInfoDAO.findAll(pageable);
    }

    @Override
    public ProductInfo save(ProductInfo productInfo) {
        return productInfoDAO.save(productInfo);
    }

    @Override
    @Transactional
    public void increaceStock(List<CartDTO> cartDTOList) {
        for (CartDTO cartDTO:cartDTOList){
            ProductInfo productInfo = productInfoDAO.findOne(cartDTO.getProductId());
            if(productInfo==null){
                throw new SellException(ProductStatus.DOWN);
            }
            Integer result = productInfo.getProductStock()+cartDTO.getProductQuantity();
            productInfo.setProductStock(result);
            productInfoDAO.save(productInfo);
        }

    }

    @Override
    @Transactional
    public void decreaseStock(List<CartDTO> cartDTOList) {
            for(CartDTO cartDTO :cartDTOList){
                ProductInfo productInfo =productInfoDAO.findOne(cartDTO.getProductId());
                if(productInfo==null){
                    throw new SellException(ProductStatus.DOWN);
                }
                Integer result = productInfo.getProductStock()-cartDTO.getProductQuantity();
                if(result<0){
                    throw  new SellException(ProductStatus.STOCK_NOT_ENOUGH);
                }
                productInfo.setProductStock(result);
                productInfoDAO.save(productInfo);
            }
    }

    @Override
    public ProductInfo onSale(String productId) {
        ProductInfo productInfo = productInfoDAO.findOne(productId);
        if (productInfo == null) {
            throw new SellException(ProductStatus.NOT_EXIST);
        }
        productInfo.setProductStatus(ProductStatus.UP.getCode());
        ProductInfo newProduct = productInfoDAO.save(productInfo);
        return newProduct;
    }

    @Override
    public ProductInfo offSale(String productId) {
        ProductInfo productInfo = productInfoDAO.findOne(productId);
        if (productInfo == null) {
            throw new SellException(ProductStatus.NOT_EXIST);
        }
        productInfo.setProductStatus(ProductStatus.DOWN.getCode());
        ProductInfo newProduct = productInfoDAO.save(productInfo);
        return newProduct;
    }
}
